﻿using Infrastructure.Centers;
using Infrastructure.Model;
using Minio;
using Minio.DataModel.Args;
using Services.IServices.IMinioFileService;

namespace Services.Services.MinioFileService
{
    [AppService(ServiceType = typeof(IMinioFileService), ServiceLifetime = LifeTime.Transient)]
    public class MinioFileService : IMinioFileService
    {
        private readonly MinioClient _minioClient;
        private readonly string _bucketName = "your-bucket-name";

        public MinioFileService(MinioClient minioClient)
        {
            _minioClient = minioClient;
        }

        // 上传文件
        public async Task<bool> UploadAsync(FileInfos fileInfo)
        {
            try
            {
                using (var stream = new MemoryStream(fileInfo.FileContent))
                {
                    var putObjectArgs = new PutObjectArgs()
                        .WithBucket(_bucketName)
                        .WithObject(fileInfo.Guid)
                        .WithStreamData(stream)
                        .WithObjectSize(fileInfo.FileSize);
                    await _minioClient.PutObjectAsync(putObjectArgs);
                }
                return true;
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine($"Upload failed: {ex.Message}");
                return false;
            }
        }

        // 下载文件
        public async Task<(bool Success, FileInfos FileInfo)> DownloadAsync(string fileGuid)
        {
            try
            {
                var getObjectArgs = new GetObjectArgs()
                    .WithBucket(_bucketName)
                    .WithObject(fileGuid);

                var memoryStream = new MemoryStream();
                await _minioClient.GetObjectAsync(getObjectArgs, (stream) =>
                {
                    stream.CopyTo(memoryStream);
                });

                memoryStream.Seek(0, SeekOrigin.Begin);

                var fileInfo = new FileInfos
                {
                    Guid = fileGuid,
                    FileContent = memoryStream.ToArray(),
                    FileSize = memoryStream.Length
                };

                return (true, fileInfo);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine($"Download failed: {ex.Message}");
                return (false, null);
            }
        }

        // 删除文件
        public async Task<bool> RemoveAsync(string fileGuid)
        {
            try
            {
                var removeObjectArgs = new RemoveObjectArgs()
                    .WithBucket(_bucketName)
                    .WithObject(fileGuid);
                await _minioClient.RemoveObjectAsync(removeObjectArgs);
                return true;
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine($"Remove failed: {ex.Message}");
                return false;
            }
        }

        // 检查文件是否存在
        public async Task<bool> IsExistsAsync(string fileGuid)
        {
            try
            {
                var statObjectArgs = new StatObjectArgs()
                    .WithBucket(_bucketName)
                    .WithObject(fileGuid);
                await _minioClient.StatObjectAsync(statObjectArgs);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        // 获取文件信息
        public async Task<(bool Success, FileInfos FileInfo)> GetInfoAsync(string fileGuid)
        {
            try
            {
                var statObjectArgs = new StatObjectArgs()
                    .WithBucket(_bucketName)
                    .WithObject(fileGuid);

                var stat = await _minioClient.StatObjectAsync(statObjectArgs);
                var fileInfo = new FileInfos
                {
                    Guid = fileGuid,
                    FileSize = stat.Size,
                    FileName = fileGuid // Assuming fileGuid as FileName here
                };
                return (true, fileInfo);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine($"GetInfo failed: {ex.Message}");
                return (false, null);
            }
        }

        // 获取多个文件信息
        public async Task<(bool Success, FileInfos[] FileInfos)> GetInfosAsync(string[] fileGuids)
        {
            try
            {
                var fileInfos = new List<FileInfos>();
                foreach (var fileGuid in fileGuids)
                {
                    var statObjectArgs = new StatObjectArgs()
                        .WithBucket(_bucketName)
                        .WithObject(fileGuid);

                    var stat = await _minioClient.StatObjectAsync(statObjectArgs);
                    fileInfos.Add(new FileInfos
                    {
                        Guid = fileGuid,
                        FileSize = stat.Size,
                        FileName = fileGuid // Assuming fileGuid as FileName here
                    });
                }
                return (true, fileInfos.ToArray());
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine($"GetInfos failed: {ex.Message}");
                return (false, null);
            }
        }
    }
}
